#include "event/loop.hpp"
#include "fmt/core.h"
#include "network/stdio_stream.hpp"
#include <string_view>

using namespace ibox::event;
using namespace ibox::network;

int main() {
    auto loop = Loop();

    StdioStream stdio{&loop};
    // 如果不想定义receive_callback, 也可以用
    // stdio.bind(&stdio); ==> 与receive_callback相比优先级更高
    stdio.set_receive_callback(
        [](Buffer &buf) {
            while (buf.readable_size() > 0) {
#if 0
                char buf2[8];
                buf.fetch(buf2, sizeof(buf2));
                fmt::print("RECV: {}\n", buf2);
#else
                // 避免拷贝
                std::string_view sv{reinterpret_cast<char *>(buf.readable_begin()), buf.readable_size()};
                fmt::print("RECV: {}\n", sv);
                buf.has_readall();
#endif
            }
        },
        /*threshold*/ 8); // 这里阈值的意思是多少个字符触发receive_callback
    stdio.enable();

    loop.run();
}
